import csv
import pprint

#Read object
exampleFile=open('example.csv')
exampleReader=csv.reader(exampleFile)
#print(exampleReader)

exampleData=list(exampleReader)
print(exampleData)
print(exampleData[0][2])

#使用pprint打印列表
pprint.pprint(exampleData)

# csv.reader指针指向文件头
exampleFile.seek(0)

#用for循环遍历整个csv文件
for row in exampleReader:
    print('Row #',exampleReader.line_num,row)

# csv文件的写
outputFile=open('output.csv','w',newline='')

outputWriter=csv.writer(outputFile)
outputWriter.writerow(['spam','eggs','bacon','ham'])
outputWriter.writerow(['Hello,world!','eggs','bacon','ham'])
outputWriter.writerow([1,2,3.141592,4])
outputFile.close()

#tsv文件的写
csvFile=open('example.tsv','w')
csvWriter=csv.writer(csvFile,delimiter='\t',lineterminator='\n')
csvWriter.writerow(['apples','oranges','grapes'])
csvWriter.writerow(['eggs','bacon','ham'])
csvFile.close()